import 'dart:developer' as developer;
import 'package:flutter/material.dart';
import 'word_import_export_util.dart';

/// 题目导入导出示例
class QuestionImportExportExample extends StatelessWidget {
  const QuestionImportExportExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('题目导入导出示例', style: TextStyle(fontFamily: 'FangSong')),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: _exportSampleQuestions,
              child: const Text('导出示例题目', style: TextStyle(fontFamily: 'FangSong')),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _importQuestions,
              child: const Text('导入题目', style: TextStyle(fontFamily: 'FangSong')),
            ),
          ],
        ),
      ),
    );
  }

  // 导出示例题目
  Future<void> _exportSampleQuestions() async {
    try {
      // 创建示例题目数据
      final questions = [
        {
          'type': 'logical_reasoning',
          'category': '逻辑推理',
          'point': '演绎推理',
          'content': '如果所有的猫都有尾巴，而汤姆是一只猫，那么可以推断出',
          'question_image_url': '',
          'option_a': '汤姆有尾巴',
          'option_a_image_url': '',
          'option_b': '汤姆没有尾巴',
          'option_b_image_url': '',
          'option_c': '不能确定汤姆是否有尾巴',
          'option_c_image_url': '',
          'option_d': '以上都不对',
          'option_d_image_url': '',
          'correct_answer': 'A',
          'explanation': '这是一个简单的三段论推理',
          'explanation_image_url': '',
        },
        {
          'type': 'quantitative_relation',
          'category': '数量关系',
          'point': '数列规律',
          'content': '找出下列数列的规律并填入问号处：2, 5, 11, 23, 47, ?',
          'question_image_url': '',
          'option_a': '23',
          'option_a_image_url': '',
          'option_b': '95',
          'option_b_image_url': '',
          'option_c': '191',
          'option_c_image_url': '',
          'option_d': '383',
          'option_d_image_url': '',
          'correct_answer': 'B',
          'explanation': '数列规律是：每项是前一项的2倍加1',
          'explanation_image_url': '',
        },
      ];

      // 导出题目
      final filePath = await WordImportExportUtil.exportQuestionsToCSV(questions);
      developer.log('题目导出成功: $filePath');
    } catch (e) {
      developer.log('导出题目失败: $e');
    }
  }

  // 导入题目
  Future<void> _importQuestions() async {
    try {
      // 导入题目
      final questions = await WordImportExportUtil.importQuestionsFromCSVOrExcel();
      developer.log('成功导入 ${questions.length} 个题目');
      
      // 打印导入的题目
      for (var question in questions) {
        developer.log('题目: ${question['content']}');
        developer.log('类型: ${question['type']}');
        developer.log('答案: ${question['answer']}');
      }
    } catch (e) {
      developer.log('导入题目失败: $e');
    }
  }
}